package cn.iocoder.yudao.module.diy.enumapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.module.diy.PosterInitParam;
import cn.iocoder.yudao.module.diy.api.GetJsonTemplateApi;
import cn.iocoder.yudao.module.diy.api.PosterDOB;
import cn.iocoder.yudao.module.diy.dal.dataobject.poster.PosterDO;
import cn.iocoder.yudao.module.diy.dal.diy.PosterTypeEnum;
import cn.iocoder.yudao.module.diy.loader.JsonModuleLoader;
import cn.iocoder.yudao.module.diy.service.poster.PosterService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;


@Service
public class GetJsonTemplateApiImpl implements GetJsonTemplateApi {
    @Resource
    private PosterService posterApi;

    @Override
    public Object getTemplate(String type) {
        if (type.isEmpty()) {
            return PosterTypeEnum.getType();
        }
        return PosterTypeEnum.getType(type);
    }

    @Override
    public PosterDOB getPoster(PosterInitParam param) {
        PosterDOB pr=new PosterDOB();
        PosterDO poster=new PosterDO();
        BeanUtils.copyProperties(param, poster);

        if (ObjectUtil.isNotEmpty(param.getId()) && param.getId() > 0) {
            poster =posterApi.getPoster(param.getId());
            Assert.notNull(poster, "海报不存在!");
        }

//        poster.setPosterType(PosterTypeEnum.getType(poster.getType()));
//        if (ObjectUtil.isNotEmpty(poster.getPosterType()) && ObjectUtil.isEmpty(poster.getAddon())) {
//            poster.setAddon(poster.getPosterType().getStr("addon", ""));
//        };

        JSONObject components = JsonModuleLoader.build().mergeResultElement("poster/components.json");
//        poster.setComponent(new JSONObject());
        for (String key : components.keySet()) {
            JSONObject item = components.getJSONObject(key);
            if (item.getJSONObject("list") == null || item.getJSONObject("list").keySet().size() == 0) {
                continue;
            }
            JSONArray support = ObjectUtil.defaultIfNull(item.getJSONArray("support"), new JSONArray());
            if (!key.equals(poster.getType()) && support.size() > 0 &&  !support.contains(poster.getType())) {
                continue;
            }
//            poster.getComponent().put(key, item);
        }
        return null;
    }
    /**
     * 海报模板
     *
     * @param addon
     * @param type
     * @return
     */
    @Override
    public JSONArray template(String addon, String type) {
        final JSONArray template = new JSONArray();
        if (ObjectUtil.isNotEmpty(addon)) {
            JSONArray coreTemplate = JsonModuleLoader.build().findResultSet("code", "poster/template.json");
            coreTemplate.stream().forEach(e -> template.add(e));

            JSONArray addonTemplate = JsonModuleLoader.build().findResultSet(addon, "poster/template.json");
            addonTemplate.stream().forEach(e -> template.add(e));
        } else {
            JSONArray all = JsonModuleLoader.build().mergeResultSet("poster/template.json");
            all.stream().forEach(e -> template.add(e));
        }

        if (ObjectUtil.isNotEmpty(type) && template.size() > 0) {
            return CollUtil.filter(template, i -> {
                JSONObject item = (JSONObject) i;
                return item.getStr("type", "").equals(type);
            });
        }

        return template;
    }

}
